﻿using System;
using System.Collections.Generic;
using NPMonitor.DataModel;
using System.Web.UI.WebControls;
using System.Drawing;

namespace NPMonitor.Web.UserControls
{
    public partial class CalendarSchedule : System.Web.UI.UserControl
    {
        public NPMonitor.DataModel.CalendarSchedule SelectedCalendarSchedule;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtStartDateTime.Text = Helpers.Utils.CurrentServerUtcTime.ToString(CommonString.DataTimeFormat);
             
            }
            else
            {
                SelectedCalendarSchedule = new DataModel.CalendarSchedule();

                #region get days of week
                HashSet<DayOfWeek> daysOfWeekBeSelected = new HashSet<DayOfWeek>();

                if (Monday.Checked)
                {
                    daysOfWeekBeSelected.Add(DayOfWeek.Monday);
                }

                if (Tuesday.Checked)
                {
                    daysOfWeekBeSelected.Add(DayOfWeek.Tuesday);
                }

                if (Thursday.Checked)
                {
                    daysOfWeekBeSelected.Add(DayOfWeek.Thursday);
                }

                if (Wednesday.Checked)
                {
                    daysOfWeekBeSelected.Add(DayOfWeek.Wednesday);
                }

                if (Friday.Checked)
                {
                    daysOfWeekBeSelected.Add(DayOfWeek.Friday);
                }

                if (Saturday.Checked)
                {
                    daysOfWeekBeSelected.Add(DayOfWeek.Saturday);
                }

                if (Sunday.Checked)
                {
                    daysOfWeekBeSelected.Add(DayOfWeek.Sunday);
                }

                SelectedCalendarSchedule.DaysOfWeek = daysOfWeekBeSelected;
                #endregion

                SelectedCalendarSchedule.StartDateTime = DateTime.ParseExact(txtStartDateTime.Text, CommonString.DataTimeFormat, null);

                #region get time to start daily
                string timeToStart = txtStartDailyAt.Text;

                TimeOfDay time = new TimeOfDay();

                if (timeToStart != string.Empty)
                {
                    time.Hour = int.Parse(timeToStart.Substring(0, 2));

                    time.Minute = int.Parse(timeToStart.Substring(timeToStart.IndexOf(":") + 1, 2));

                    time.Second = int.Parse(timeToStart.Substring(timeToStart.LastIndexOf(":") + 1));

                    SelectedCalendarSchedule.DailyStartTime = time;
                }
                #endregion

                #region get the repeat interval

                TimeSpan repeatInterval = new TimeSpan();

                string strRepeatInterval = txtRepeatInterval.Text;

                if (strRepeatInterval != string.Empty && TimeSpan.TryParse(strRepeatInterval, out repeatInterval))
                {
                    SelectedCalendarSchedule.RepeatInterval = repeatInterval;
                }

                #endregion
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            cvDailyAt.ErrorMessage = string.Format(ValidationString.FieldInvalid, "Daily Start Time");
            cvDaysOfWeek.ErrorMessage = string.Format(ValidationString.FieldRequired, "Week Select");
            cvRepeatInterval.ErrorMessage = string.Format(ValidationString.FieldInvalid, "Repeat Interval");
            //cvStratTime.ErrorMessage = string.Format(ValidationString.FieldInvalid, "Start Time");
        }

        
    }
}